home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ASME's Mechanical Engine…ing Toolkit 1997 December
/
ASME's Mechanical Engineering Toolkit 1997 December.iso
/
ai
/
prlg195b.lzh
/
GAMES.LZH
/
KTOUR.DOC
< prev
next >
Wrap
Text File
|
1987-05-15
|
7KB
|
110 lines
THE KNIGHT'S TOUR
The "knight's tour" is a classic chess puzzle that may be
enjoyed and appreciated even without a deep knowledge of the game
of chess. As with so many other puzzles of lasting appeal, the
object is easy to state and understand, but finding a solution can
involve unexpected insights and ingenuity. This problem has
interested many prominent mathematicians since at least the
nineteenth century; for a survey discussion of well-known solu-
tions, see one of these:
Mathematical Recreations & Essays, W. W. R. Ball, MacMillan
and Co. Ltd. (1st edition was 1892, but it was
repeatedly reprinted, your library should have a copy).
"Mathematical Games: Problems that are built on the knight's
move in chess", Martin Gardner, Scientific American, Oct.
1967, pp. 128-132.
The object is as follows: a knight is placed on an arbitrary
square on a chessboard, and must then be moved to visit each of
the remaining squares exactly once, eventually visiting the entire
board without repeating any square on the path. (A knight's move
is "L" shaped, two squares up, down, left or right, then one
square in a direction perpendicular to the first; see any intro-
ductory chess book).
There are literally millions of correct solutions, but finding
even one of these paths by naive trial-and-error will soon frus-
trate the impatient. However, the Prolog language would seem to
be ideally suited to solving this sort of task, as a program could
arbitrarily choose successive legal knight's moves, backtracking
whenever it got "stuck" in order to try new alternatives until it
covered the whole board. The first version of my program took
this approach, but would literally run for hours without "acciden-
tally" stumbling onto even a single solution for a full-size
(8 by 8) chess board, although it did manage to find several
solutions for a smaller 5 by 5 board.
The problem was, uninspired guessing led it to construct
candidate solutions that had obvious blunders in the first twenty
or so moves. Even a moderately sophisticated human observer would
quickly recognize the futility of attempting to complete such
paths, but the program blindly went about trying to complete them
anyway. True, backtracking would eventually lead to a correct
solution, but this was slow, since it tried every possibility
exhaustively. But I am not the first to note that it is one thing
to state a logical goal and quite another for Prolog to satisfy it
in practice, especially where recursion is involved.
To understand the approach taken by the revised program, it is
instructive first to examine one class of the obvious blunders
mentioned above. As a correct solution passes through every
square on the board, it must at some point include all four
corners. Choose an arbitrary corner of a chessboard; it is easy
to convince yourself that there are only two squares that are a
legal knight's move away (label them A and B). Suppose that in
constructing a candidate solution, the program visits square A.
Then if it does not choose to visit the corner on the next move
and continue the path via square B, it has committed itself to
ending the tour in this corner. (It must approach the corner
through square B at some later point in the tour. Once there, it
cannot exit the corner to visit other squares, since A and B have
already been covered, and they are the only ways out). If the
program should similarly commit itself to ending in some other
corner as it attempts to trace a solution path, then clearly it
has blundered, since it cannot end the solution in both places.
Considerations such as this give rise to the "Warnsdorff
rule," first proposed in print by the mathematician H. C. Warns-
dorff in 1823. It states that whenever you must choose between
two or more possible next moves in constructing a solution, you
should move to the square that provides the fewest alternative
moves for the next turn. Unfortunately, statements of this rule
by more contemporary authors do not agree on whether Warnsdorff
said to "look ahead" one move or two (I have been unable to find,
much less READ the original German). Confronted with the "corner"
situation described above, the rule would likely trace a path
through the corner and back out again, and so avoid making unnec-
cessary commitments.
My program implements the simplest form of this rule, looking
ahead only a single move at each step. When faced with a choice,
it constructs a list of possibilities, beginning with those moves
that will lead to the fewest legal alternatives on the next move.
By trying the choices on the list in this order, the program
attempts to follow up on those paths that are most likely to be
fruitful first, according to the Warnsdorff rule. If the rule
ranks two possible moves equally, the program chooses between them
arbitrarily. (In practice, it picks the one that was generated
first by the "knight's move" rules in the database. I have
numbered these for reference from 1 to 8, beginning at the upper
right, as on a clock face. I would love to hear from anybody who
has any thoughts on what would be the best order for listing these
rules, or who has other ideas for tie-breaking).
Although the extra computation involved in using the rule
rather than choosing the next move arbitrarily appears to slow the
program down, it will now often find a correct solution on the
first try. If you want, it will backtrack and try to find more
alternative solutions by exploring the remaining moves on the
sorted move list at each step. The graphics that accompany this
search process are a nice visual metaphor of the more general
Prolog backtracking search technique for satisfying complex goals.
For instructions on running the program, read the comment at
the beginning of the file. I hope you will find it enjoyable, and
will be inspired to read more in the vast literature surrounding
this problem.
Tim Elliott
125 Warren Avenue
Rochester, NY 14618